<%--
*
* 生成数据
*
--%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.sky.fisher.query.PageResult" %>
<%@ page import="com.sky.fisher.model.GridModel" %>
<%@ page import="com.sky.fisher.service.FisherService" %>
<%@ page import="com.sky.common.http.HTTPUtils"%>
<%@ page import="org.apache.commons.logging.Log"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:directive.page import="com.sky.fisher.db.AbstractConnectionProvider"/>
<jsp:directive.page import="com.toncen.samepms.fisher.query.MemoryConnectionProvider"/>
<jsp:directive.page import="com.sky.common.json.JSONUtils"/>
<jsp:directive.page import="com.sky.fisher.model.CommonColumnModel"/>
<jsp:directive.page import="com.sky.fisher.model.CheckboxColumnModel"/>
<%
	Log logger = LogFactory.getLog(FisherService.class);
	Map<String, String[]> paramters = null;
	String strSQL = null;
	try{
	paramters = request.getParameterMap(); 
	logger.debug("Grid：\n"+paramters.get(FisherService.PARAMETER_GRID)[0]);
	logger.debug("参数：\n"+HTTPUtils.getParameterMap(request));
	long startTime=System.currentTimeMillis();
	GridModel grid = FisherService.getGrid(paramters.get(FisherService.PARAMETER_GRID)[0]); 
	grid.setRequest(request);
	strSQL = FisherService.getSQL(grid,paramters); 
	int pagenumber = Integer.parseInt(paramters.get(FisherService.PARAMETER_PAGE)[0]); 
	int pagesize = Integer.parseInt(paramters.get(FisherService.PARAMETER_PAGESIZE)[0]); 
    AbstractConnectionProvider connectionProvider=new MemoryConnectionProvider(request);
    connectionProvider.setGrid(grid);
    PageResult pageResult =connectionProvider.getQuery().executeQuery(strSQL, pagenumber, pagesize);
	String jsonresult = FisherService.getJSONResult(grid, startTime,pageResult,false); 
	if("true".equalsIgnoreCase(request.getParameter("readonly"))){//只读状态
		List<Integer> indexs=new ArrayList<Integer>();
		int index=0;
		for (CommonColumnModel column : grid.getColumnList()) {//查找所有的选择列
			if ("false".equalsIgnoreCase(column.getVisual())) {// 隐藏的不添加到结果集中
				continue;
			}	
			if (column instanceof CheckboxColumnModel) {// 选择列
				indexs.add(index);
			}
			index++;
		}
		if(indexs.size()!=0){//有选择列，则移除内容
			Map<String, Object> JSONResult =(Map<String, Object>)JSONUtils.JSON2object(jsonresult);
			List<Map<String, Object>> rows = (List<Map<String, Object>>)JSONResult.get("rows");	
			for(Map<String, Object> row:rows){
				List<String> cell =(List<String>)row.get("cell");
				for(int i=indexs.size()-1;i>=0;i--){
					cell.remove(indexs.get(i).intValue());
				}
			}
			jsonresult=JSONUtils.object2JSON(JSONResult);
		}
	}	
	response.setContentType("text/html"); 
	out.println(jsonresult); 
	}catch(Throwable e){		
	  	e.printStackTrace();
	  	logger.error("Grid：\n"+paramters.get(FisherService.PARAMETER_GRID)[0]);
	  	logger.error("参数：\n"+paramters);
	  	logger.error("SQL：\n"+strSQL);
	  	logger.error(e.getMessage(),e);
		throw e;
	}
%>	
